package model;

import java.io.Serializable;
import java.util.Random;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@SuppressWarnings("serial")
@Entity
@Table(name = "agentinfo")
public class AgentInfo implements Serializable {
	
	@Id
	@Column(name = "aid", unique = true, nullable = false)
	private int AID=0;
	
	@Column(name = "localhost")
	private Host localHost;
	
	@Column(name = "agenttype")
	private AgentType agentType;
	
	/**
	 * Recommended, use this constructor
	 * @param localHost
	 * @param agentType
	 */
	public AgentInfo(Host localHost, AgentType agentType) {
		//++AID;
		AID=new Random().nextInt()%1000;
		this.localHost=localHost;
		this.agentType=agentType;
	}
	
	/**
	 * This constructor allows for full customization
	 * @param localHost
	 * @param agentType
	 * @param AID Agent ID
	 */
	public AgentInfo(Host localHost, AgentType agentType, int AID) {
		//++AID;
		//AID=new Random().nextInt()%1000;
		this.localHost=localHost;
		this.agentType=agentType;
		this.AID=AID;
	}
	
	public AgentInfo() {
		//++AID;
		AID=new Random().nextInt()%1000;
		this.localHost=new Host();
		this.agentType=AgentType.MERCHANT;
	}
	

	public int getAID() {
		return AID;
	}
	
	public void setAID(int AID) {
		this.AID=AID;
	}

	public Host getLocalHost() {
		return localHost;
	}

	public void setLocalHost(Host localHost) {
		this.localHost = localHost;
	}

	public AgentType getAgentType() {
		return agentType;
	}

	public void setAgentType(AgentType agentType) {
		this.agentType = agentType;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + AID;
		result = prime * result
				+ ((localHost == null) ? 0 : localHost.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AgentInfo other = (AgentInfo) obj;
		if (AID != other.AID)
			return false;
		if (localHost == null) {
			if (other.localHost != null)
				return false;
		} else if (!localHost.equals(other.localHost))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return AID+"."+agentType+"@"+localHost;
	}
}
